<template>
  <div class="user-manage-view">
    <div class="user-manage-left">
      <div class="user-manage-left-top">
        <div class="button-box">
          <el-button type="primary" class="add-button" @click="addUser">新增用户</el-button>
        </div>
        <div class="left-top-seach">
          <el-input placeholder="请输入内容" v-model="userSeach" class="input-with-select">
            <el-button slot="append" @click="findUser">查询</el-button>
          </el-input>
        </div>
      </div>
      <div class="user-manage-left-bottom"
           v-loading="isLoading"
           element-loading-text="数据加载中"
           element-loading-spinner="el-icon-loading"
           element-loading-background="rgba(21, 29, 54, 0.8)"
      >
        <div class="user-table">
          <el-table
            :data="tableData"
            height="100%"
            border
            style="width: 100%"
            highlight-current-row
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            @row-click="rowClickUser"
          >
            <el-table-column
              align="center"
              :show-overflow-tooltip="true"
              prop="index"
              label="序号"
              width="80">
            </el-table-column>
            <el-table-column
              align="center"
              :show-overflow-tooltip="true"
              prop="name"
              label="用户名"
              width="100">
            </el-table-column>
            <el-table-column
              align="center"
              :show-overflow-tooltip="true"
              prop="description"
              label="用户描述"
            >
            </el-table-column>
            <el-table-column
              align="center"
              :show-overflow-tooltip="true"
              prop="poName"
              label="所属岗位"
            >
            </el-table-column>
            <el-table-column
              align="center"
              :show-overflow-tooltip="true"
              prop="inpositionName"
              label="就职岗位"
            >
            </el-table-column>
            <!--<el-table-column-->
              <!--align="center"-->
              <!--:show-overflow-tooltip="true"-->
              <!--prop="cardNum"-->
              <!--label="e卡通"-->
            <!--&gt;-->
            <!--</el-table-column>-->
            <!--<el-table-column-->
              <!--align="center"-->
              <!--:show-overflow-tooltip="true"-->
              <!--prop="passNum"-->
              <!--label="出入证号"-->
            <!--&gt;-->
            <!--</el-table-column>-->
            <el-table-column
              align="center"
              :show-overflow-tooltip="true"
              prop="jobNum"
              label="员工证号"
            >
            </el-table-column>
            <el-table-column label="操作" width="180" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="editUser(scope.$index, scope.row)">编辑
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="delUser(scope.$index, scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="user-paging">
          <el-pagination
            layout="total, sizes, prev, pager, next"
            background
            :page-size="pageSize"
            :total="total"
            :page-count="10"
            :page-sizes="[5, 10, 20, 40]"
            :current-page="currentPage"
            @current-change="changePage"
            @size-change="sizeChange"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="user-manage-right">
      <div class="user-manage-right-top">
        <el-button type="primary" class="edit-button" @click="addNewRole" v-if="isFirst"
                   :disabled="selectItemIndex == -1 ? true : false">配置角色
        </el-button>
        <el-button type="primary" class="edit-button edit-system-button" @click="makeSysUser"
                   :disabled="selectItemIndex == -1 ? true : false" v-else>开通系统账号
        </el-button>
      </div>
      <div class="user-manage-right-bottom">
        <div class="user-role-content">
          <el-tabs v-model="activeName" type="border-card" @tab-click="tabsClick">
            <el-tab-pane label="角色管理" name="first">
              <div class="user-manage-content">
                <el-table
                  :data="tableDataRole"
                  height="100%"
                  border
                  style="width: 100%"
                  highlight-current-row
                  :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                >
                  <el-table-column
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="index"
                    label="序号"
                    width="80">
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="name"
                    label="角色名称"
                    width="120">
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="desciption"
                    label="角色描述"
                  >
                  </el-table-column>
                </el-table>
              </div>
            </el-tab-pane>
            <el-tab-pane label="账号管理" name="second">
              <div class="configure-manage-content">
                <el-table
                  :data="tableDataSystem"
                  height="100%"
                  border
                  style="width: 100%"
                  highlight-current-row
                  :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                >
                  <el-table-column
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="index"
                    label="序号"
                    width="80">
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="loginname"
                    label="账号名称"
                  >
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="type"
                    label="账号类型"
                  >
                  </el-table-column>
                  <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        @click="editSysUser(scope.$index, scope.row)">编辑
                      </el-button>
                      <el-button
                        size="mini"
                        type="danger"
                        @click="delSysUser(scope.$index, scope.row)">删除
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
    <el-dialog title="新增用户" :visible="dialogAddUser" width="30%" @close="addUserClose" :close-on-click-modal="false">
      <el-form :model="userForm" ref="addUserForm" :rules="adduserrules">
        <el-form-item label="用户名称" label-width="80px" prop="name">
          <el-input v-model="userForm.name" autocomplete="off" placeholder="请输入用户名称"></el-input>
        </el-form-item>
        <el-form-item label="用户描述" label-width="80px">
          <el-input v-model="userForm.desc" autocomplete="off" placeholder="请输入用户描述"></el-input>
        </el-form-item>
        <el-form-item label="用户电话" label-width="80px" prop="phone">
          <el-input v-model="userForm.phone" autocomplete="off" placeholder="请输入用户电话"></el-input>
        </el-form-item>
        <el-form-item label="所属岗位" label-width="80px" prop="station">
          <el-select
            v-model="userForm.station"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请选择岗位"
            @change="changeStation"
          >
            <el-option
              v-for="item in stationList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就职岗位" label-width="80px" prop="inposition">
          <el-select
            v-model="userForm.inposition"
            placeholder="请选择就职岗位">
            <el-option
              v-for="item in joinStationList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="e卡通" label-width="80px" prop="cardNum">-->
          <!--<el-input v-model="userForm.cardNum" autocomplete="off" placeholder="请输入e卡通号"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="出入证号" label-width="80px" prop="passNum">-->
          <!--<el-input v-model="userForm.passNum" autocomplete="off" placeholder="请输入出入证号"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="员工工号" label-width="80px" prop="jobNum">
          <el-input v-model="userForm.jobNum" autocomplete="off" placeholder="请输入员工工号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--<el-button @click="addUserCancel">取 消</el-button>-->
        <el-button type="primary" @click="addUserSure">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑用户" :visible="dialogEditUser" width="30%" @close="editUserClose" :close-on-click-modal="false">
      <el-form :model="userForm" ref="editUserForm" :rules="adduserrules">
        <el-form-item label="用户名称" label-width="80px" prop="name">
          <el-input v-model="userForm.name" autocomplete="off" placeholder="请输入用户名称"></el-input>
        </el-form-item>
        <el-form-item label="用户描述" label-width="80px">
          <el-input v-model="userForm.desc" autocomplete="off" placeholder="请输入用户描述"></el-input>
        </el-form-item>
        <el-form-item label="用户电话" label-width="80px" prop="phone">
          <el-input v-model="userForm.phone" autocomplete="off" placeholder="请输入用户电话"></el-input>
        </el-form-item>
        <el-form-item label="所属岗位" label-width="80px" prop="station">
          <el-select
            v-model="userForm.station"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请选择岗位"
            @change="changeStation"
          >
            <el-option
              v-for="item in stationList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就职岗位" label-width="80px" prop="inposition">
          <el-select
            v-model="userForm.inposition"
            placeholder="请选择就职岗位">
            <el-option
              v-for="item in joinStationList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="e卡通" label-width="80px" prop="cardNum">-->
          <!--<el-input v-model="userForm.cardNum" autocomplete="off" placeholder="请输入e卡通号"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="出入证号" label-width="80px" prop="passNum">-->
          <!--<el-input v-model="userForm.passNum" autocomplete="off" placeholder="请输入出入证号"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="员工工号" label-width="80px" prop="jobNum">
          <el-input v-model="userForm.jobNum" autocomplete="off" placeholder="请输入员工工号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editUserKeep">保存</el-button>
        <!--<el-button type="warning" @click="delUser">删除</el-button>-->
      </div>
    </el-dialog>
    <el-dialog title="配置角色" :visible="dialogEditRoles" width="30%" @close="addRolesClose" :close-on-click-modal="false">
      <el-form :model="rolesForm">
        <el-form-item label="角色" label-width="50px">
          <el-select v-model="rolesForm.roles" multiple placeholder="请选择角色">
            <el-option
              v-for="item in rolesList"
              :key="item.value"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addRolesKeep">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="开通系统账号" :visible="dialogSysUser" width="30%" @close="sysUserClose" :close-on-click-modal="false">
      <el-form :model="sysUserForm" ref="addSysUserForm" :rules="sysUserrules">
        <el-form-item label="登录名称" label-width="80px" prop="name">
          <el-input v-model="sysUserForm.name" autocomplete="off" placeholder="请输入登录名称"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" label-width="80px" prop="password">
          <el-input v-model="sysUserForm.password" autocomplete="off" placeholder="请输入登录密码"></el-input>
        </el-form-item>
        <el-form-item label="重复密码" label-width="80px" prop="agamePassword">
          <el-input v-model="sysUserForm.agamePassword" autocomplete="off" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item label="账号类型" label-width="80px" prop="type">
          <el-select
            v-model="sysUserForm.type"
            filterable
            allow-create
            default-first-option
            placeholder="请选择账户类型">
            <el-option
              v-for="item in sysUserTypeList"
              :key="item.id"
              :label="item.datacode"
              :value="item.datavalue">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addSysUserKeep">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑系统账号" :visible="dialogSysUserEdit" width="30%" @close="sysUserEditClose"
               :close-on-click-modal="false">
      <el-form :model="sysUserForm" ref="editSysUserForm" :rules="sysUserrules">
        <el-form-item label="登录名称" label-width="80px" prop="name">
          <el-input v-model="sysUserForm.name" autocomplete="off" placeholder="请输入登录名称"></el-input>
        </el-form-item>
        <el-form-item label="密码验证" label-width="80px" prop="password">
          <el-input v-model="sysUserForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="账号类型" label-width="80px" prop="type">
          <el-select
            v-model="sysUserForm.type"
            filterable
            allow-create
            default-first-option
            placeholder="请选择账号类型">
            <el-option
              v-for="item in sysUserTypeList"
              :key="item.id"
              :label="item.datacode"
              :value="item.datavalue">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sysUserEditKeep">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  export default {

    components: {
      // name
    },
    name: "userManageView",
    props: {
      isLoading: {
        type: Boolean,
        default: false
      },
      total: {
        type: Number,
        default: 0
      },
      pageSize: {
        type: Number,
        default: 10
      },
      tableData: {
        type: Array,
        default: []
      },
      tableDataRole: {
        type: Array,
        default: []
      },
      tableDataSystem: {
        type: Array,
        default: []
      },
      isFirst: {
        type: Boolean,
        default: true
      },
      dialogAddUser: {
        type: Boolean,
        default: false
      },
      dialogEditUser: {
        type: Boolean,
        default: false
      },
      userForm: {
        type: Object,
        default: {}
      },
      stationList: {
        type: Array,
        default: []
      },
      selectItemData: {
        type: Object,
        default: {}
      },
      selectItemIndex: {
        type: String,
        default: "-1"
      },
      currentPage: {
        type: Number,
        default: 1
      },
      dialogEditRoles: {
        type: Boolean,
        default: false
      },
      rolesForm: {
        type: Object,
        default: {}
      },
      rolesList: {
        type: Array,
        default: []
      },
      dialogSysUser: {
        type: Boolean,
        default: false
      },
      sysUserForm: {
        type: Object,
        default: {}
      },
      sysUserTypeList: {
        type: Array,
        default: []
      },
      dialogSysUserEdit: {
        type: Boolean,
        default: false
      },
      joinStationList: {
        type: Array,
        default: []
      }
      // msg: String //  [name: typeof]
    },
    filters: {},
    computed: {},
    watch: {},
    data() {
      let nameRules = (rule, value, callback) => {
        let reg = /^[^\d.\-\[\]{}+=_;:'"<>?/\\，,。、‘’“”；：【】|——]/;
        // let reg = /^[^\d.\-\[\]{}+=_;:'"<>?/\\，,。、‘’“”；：【】|——]+\d$/;
        if (!value) {
          return callback(new Error('角色名不能为空'));
        } else if (!reg.test(value)) {
          return callback(new Error('角色名不能以特殊字符和数字开头'));
        } else {
          callback();
        }
      };

      let phone = (rule, value, callback) => {
        // let reg = /^1[3-9]\d{9}/;
        let reg = /^((0\d{2,3}-\d{7,8})|(1[3-9]\d{9}))$/;
        if (!reg.test(value)) {
          return callback(new Error('请输入正确的手机号码'));
        } else {
          callback();
        }
      };

      let jobNum = (rule, value, callback) => {
        let reg = /^\w+$/;
        if (!reg.test(value)) {
          return callback(new Error('请输入正确的员工工号，由数字、字母、下划线组成'));
        } else {
          callback();
        }
      };

      let name = (rule, value, callback) => {
        let reg =  /[\u4E00-\u9FA5]/g;
        if (reg.test(value)) {
          return callback(new Error('账号不能使用中文'));
        } else {
          callback();
        }
      };


      return {
        userSeach: '',
        activeName: 'first',
        adduserrules: {
          name: [
            {required: true, validator: nameRules, trigger: 'blur'},
            {min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}
          ],
          phone: [
            {validator: phone, trigger: 'change'},
          ],
          station: [
            {required: true, message: '请选择应用', trigger: 'change'}
          ],
          inposition: [
            {required: true, message: '请输入路径', trigger: 'change'}
          ],
          cardNum: [
            {required: true, message: '请输入e通卡号', trigger: 'blur'}
          ],
          passNum: [
            {required: true, message: '请输入出入证号', trigger: 'blur'}
          ],
          jobNum: [
            {required: true, validator: jobNum, trigger: 'blur'}
          ]
        },
        sysUserrules: {
          name: [
            {required: true, validator: name, trigger: 'blur'},
          ],
          password: [
            {required: true, message: '请输入输入密码', trigger: 'blur'},
          ],
          agamePassword: [
            {required: true, message: '请再次输入密码', trigger: 'blur'},
          ],
          type: [
            {required: true, message: '请选择账户类型', trigger: 'change'}
          ]
        }
      }
    },
    created() {

    },
    mounted() {

    },
    methods: {
      addUser() {
        this.$emit('addUser');
      },
      addUserCancel() {
        this.$emit('addUserCancel');
      },
      addUserSure() {
        this.$emit('addUserSure');
      },
      addUserClose() {
        this.$emit('addUserClose');
      },
      editUser(index, row) {
        this.$emit('editUser', index, row);
      },
      editUserKeep() {
        this.$emit('editUserKeep');
      },
      delUser(index, row) {
        this.$emit('delUser', index, row);
      },
      editUserClose() {
        this.$emit('editUserClose');
      },
      findUser() {
        this.$emit('findUser', this.userSeach);
      },
      changePage(page) {
        this.$emit('changePage', page);
      },
      sizeChange(num) {
        this.$emit('sizeChange', num);
      },
      addNewRole() {
        this.$emit('addNewRole');
      },
      tabsClick(tab, event) {
        this.$emit('tabsClick', tab, event);
      },
      rowClickUser(row, column, event) {
        this.$emit('rowClickUser', row, column, event);
      },
      userRoleDelete(index, row) {
        this.$emit('userRoleDelete', index, row);
      },
      addRolesClose() {
        this.$emit('addRolesClose');
      },
      addRolesKeep() {
        this.$emit('addRolesKeep');
      },
      makeSysUser() {
        this.$emit('makeSysUser');
      },
      sysUserClose() {
        this.$emit('sysUserClose');
      },
      addSysUserKeep() {
        this.$emit('addSysUserKeep');
      },
      delSysUser(index, row) {
        this.$emit('delSysUser', index, row);
      },
      editSysUser(index, row) {
        this.$emit('editSysUser', index, row);
      },
      sysUserEditClose() {
        this.$emit('sysUserEditClose');
      },
      sysUserEditKeep() {
        this.$emit('sysUserEditKeep');
      },
      changeStation(arr) {
        this.$emit('changeStation', arr);
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">
  @import url("./userManage");
</style>
